import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import('../components/Login.vue')
const Home = () => import('../components/Home.vue')
const User = () => import('../components/User/User.vue')
const Rights = () => import('../components/Rights/Rights.vue')
const Roles = () => import('../components/Rights/Roles.vue')
const Report = () => import('../components/report/Report.vue')
const Category = () => import('../components/Goods/Category.vue')
Vue.use(VueRouter)

const routes = [{
  path: '/Login',
  component: Login
},
{
  path: '/',
  redirect: '/Login'
},
{
  path: '/home',
  component: Home,
  redirect: '/welcome',
  children: [{
    path: '/welcome',
    component: () => import('../components/Welcome.vue')
  },
  {
    // 绑定了接口规定的名字 element里面绑定了item.path
    path: '/users',
    component: User
  },
  {
    path: '/rights',
    component: Rights
  },
  {
    path: '/roles',
    component: Roles
  },
  {
    path: '/reports',
    component: Report
  },
  {
    path: '/categories',
    component: Category
  }
  ]
}
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  /*
    to:要去访问哪个页面
    from：从哪个页面来
    next:放行  next() 放行 next('/')跳转到某个页面
  */
  if (to.path === '/Login') return next()
  // token不存在 跳转到Login
  if (!window.sessionStorage.getItem('token')) { return next('/Login') }
  next()
})
export default router
